/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fatecpg.songs.site.managedbeans;

import br.com.fatecpg.songs.dao.hibernate.SongDAO;
import br.com.fatecpg.songs.dao.util.DAOException;
import br.com.fatecpg.songs.dao.util.DAOFactory;
import br.com.fatecpg.songs.entity.Song;
import br.com.fatecpg.songs.util.FacesContextUtil;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 *
 * @author Eloyza
 */
@ManagedBean(name = "songSearch")
@RequestScoped
public class SongManagedBean {

    private Song song;
    private final SongDAO songDAO;

    public SongManagedBean() throws DAOException {
        songDAO = (SongDAO) DAOFactory.getDAO(Song.class);
        song = getSongSearch();


    }

    public List<Song> getResult() {
        String search = FacesContextUtil.getParameter("search");
        if (search != null) {
            return songDAO.search(search);
        } else {
            return new ArrayList<Song>();
        }
    }

    private Long getId() {
        if (FacesContextUtil.getParameter("idSong") != null) {
            Long id = Long.parseLong(FacesContextUtil.getParameter("idSong"));
            return id;
        }
        return null;
    }

    private Song getSongSearch() {
        if (getId() != null) {
            song = songDAO.load(getId());
            return song;
        }
        return null;
    }

    public Song getSong() {
        return song;
    }

    public void setSong(Song song) {
        this.song = song;
    }

}
